<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>添加/编辑导入餐品信息excel</span>
        <span
          class="el-icon-circle-close close-Btn-line"
          @click="closePOpup"
        ></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="120px" ref="editForm">
          <el-row>
            <el-col :span="16">
              <el-form-item label="选择对应学校:" prop="class_id">
				<el-select clearable v-model="editForm.school_id" placeholder="选择学校"  style="width:100%">
					<el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id">{{item.name}}</el-option>
				</el-select>
              </el-form-item>
            </el-col>
          </el-row>
          
          <el-form-item label="选择excel文件:" prop="pic">
            <el-upload
              class="upload-demo"
              :action="$uploadImg"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :before-upload="beforeAvatarUpload"
              :file-list="fileList"
              multiple
              accept=".xlsx,.xls"
              :limit="1"
            >
              <el-button size="small" class="el-icon-upload"
                >点击上传</el-button
              >
              <div slot="tip" class="el-upload__tip">
                只能上传excel文件，且不超过500kb
              </div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button
          @click="submitFood"
          size="small"
          type="primary"
         
        >
          确定并保存
        </el-button>
        <el-button size="small" @click="closePOpup">返回</el-button>
      </div>
    </div>
  </div>
</template>
  
<script>
import {school_list,release_import_student} from '@/api/admin/food'
export default {
  props: ["visible"],
  components: {
  },
  data() {
    return {
      schoolList:[],
      editForm: {
        school_id: '', // 学校id
        file: '', // 文件地址
      }, // 编辑界面数据
      dialogVisible: false,
      fileList:[],
      activeName: "first",
    };
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible;
    },
  },
  created() {
    this.getSchoolFn()
  },
  methods: {
    handleRemove (file, fileList) {
      console.log('移除', file, fileList)
      this.editForm.file = ''
    },
    handleSuccess (response) {
      this.editForm.file = response.data.name
    },
    beforeAvatarUpload (file) {
      console.log('上传数据excel：', file)
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    },
    // 获取学校信息
    getSchoolFn(){
        school_list().then(res=>{
            console.log(res,'学校res-----------')
            if(res.code === 0){
                this.schoolList = res.list
            }
        }).catch(err=>{
            console.log(err,'eerr')
        })
    },
    submitFood() {
        console.log(this.editForm)
      if (!this.editForm.school_id) {
        this.$message.error('选择学校')
        return
      }
      if (!this.editForm.file) {
        this.$message.error('请上传相应的Excel文件')
        return
      }
      release_import_student(this.editForm).then(res=>{
        console.log(res,'学校模板打印res-----------')
            if(res.code === 0){
                this.$message.success('导入成功!')
            }else{
                this.$message.error(res.msg)
            }
            setTimeout(()=>{
                this.$emit('refreshList')
                this.closePOpup()
            },2000)
      }).catch(err=>{
        this.$message.error(err)

        console.log(err,'err学校报错导入')
      })
    },
    getNewFn() {
      this.$emit("InitFn");
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit("update:visible", false);
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option));
    },
  },
  mounted() {},
};
</script>
  <style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 50%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}
</style>
  
  
  